package com.apobates.jforum2.troll.forum.mapper;

import com.apobates.jforum2.troll.forum.entity.Board;
import com.apobates.jforum2.troll.forum.stub.vo.BoardVo;
import com.apobates.jforum2.troll.regular.ForumEntityStatusEnum;
import com.apobates.jforum2.troll.utils.core.api.OptionalEntityTransformMapper;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 *
 * @author xiaofanku
 */
public class BoardVoMapperEntity implements OptionalEntityTransformMapper<Board, BoardVo> {

    public static BiFunction<Long, Integer, String> produceLink = (Long boardId, Integer boardgroupId) -> {
        return String.format("/board/%d-%d.xhtml", boardId, boardgroupId);
    };

    @Override
    public Optional<BoardVo> transform(Optional<Board> entity) {
        return entity.map(board -> {
            return mapper().apply(board);
        });
    }

    /**
     * 转码ImageAddr
     *
     * @since 20201125
     * @return
     */
    public static Function<Board, BoardVo> mapper() {
        return (board) -> {
            BoardVo b = new BoardVo();
            b.setId(board.getId());
            b.setTitle(board.getTitle());
            b.setDescription(board.getDescription());
            b.setEntryDateTime(board.getEntryDateTime());
            b.setImageAddr(board.getImageAddr());
            b.setVolumes(board.getVolumesId());
            b.setStatus(board.getStatus().getSymbol());
            b.setStatusTitle(board.getStatus().getTitle());
            b.setRanking(board.getRanking());
            b.setLink(BoardVo.produceLink.apply(b.getId(), b.getVolumes()));
            b.setIcoStatus(getIcoStatus(board.getStatus()));
            return b;
        };
    }

    private static String getIcoStatus(ForumEntityStatusEnum status) {
        if (ForumEntityStatusEnum.LOCKED == status) {
            return "locked"; //锁定
        }
        if (ForumEntityStatusEnum.DELETE == status) {
            return "read"; //删除
        }
        return "normal"; //正常
    }
}
